/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.eclipse.jadex.adfmanager.wizards;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

/**
 * This page allows to get the following information in order to create a new event:
 *  - Event type
 *  - Event name
 *  - Event flags
 *  
 *  @author Jorge Valhondo Rama
 *  @author Pablo Muñoz Galindo
 */

public class AddEventWizardPage1 extends WizardPage {
	
	private Text nameText;
	private Combo typeCombo, postToAllCombo, /*randomSelCombo,*/ messageEventTypeCombo;
	private Button enablePostToAll/*, enableRandomSel*/;
	
	/**
	 * Constructor for AddEventWizardPage1
	 * 
	 * @param pageName
	 */
	public AddEventWizardPage1(ISelection selection) {
		super("wizardPage");
		setTitle("Add Event Wizard");
		setDescription("Page 1: Event attributes");
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout(2, true);
		container.setLayout(layout);	
		layout.numColumns = 4;
		layout.horizontalSpacing = 50;
		layout.verticalSpacing = 9;
		PlatformUI.getWorkbench().getHelpSystem().setHelp(container, "es.upm.dit.gsi.eclipse.jadex.adfmanager.addEventWizard");
		
		Label label1 = new Label(container, SWT.NULL);
		label1.setText("&Type:");
		typeCombo = new Combo(container, SWT.READ_ONLY);
		typeCombo.setItems (new String [] {"internalevent", "messageevent"});
		typeCombo.select(1);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 3;
		typeCombo.setLayoutData(gd);
		typeCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		
		Label label2 = new Label(container, SWT.NULL);
		label2.setText("&Name:");
		nameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 3;
		nameText.setLayoutData(gd);
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label label3 = new Label(container, SWT.NULL);
		label3.setText("&Type of message event:");
		messageEventTypeCombo = new Combo(container, SWT.SIMPLE);
		messageEventTypeCombo.setItems (new String [] {"fipa"});
		messageEventTypeCombo.select(1);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 3;
		messageEventTypeCombo.setLayoutData(gd);
		messageEventTypeCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label separator = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		separator.setLayoutData(gd);
		gd.horizontalSpan = 4;	
		
		
		Label label4 = new Label(container, SWT.NULL);
		label4.setText("&posttoall:");
		postToAllCombo = new Combo(container, SWT.READ_ONLY);
		postToAllCombo.setItems (new String [] {"false", "true"});
		postToAllCombo.select(0);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;	
		postToAllCombo.setLayoutData(gd);
		postToAllCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		enablePostToAll = new Button (container, SWT.CHECK);
		enablePostToAll.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		
		/*
		Label label5 = new Label(container, SWT.NULL);
		label5.setText("&randomsel:");
		randomSelCombo = new Combo(container, SWT.READ_ONLY);
		randomSelCombo.setItems (new String [] {"false", "true"});
		randomSelCombo.select(0);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;	
		randomSelCombo.setLayoutData(gd);
		randomSelCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		enableRandomSel = new Button (container, SWT.CHECK);
		enableRandomSel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				enabledPropertiesChanged();
			}
		});
		*/
		
		initialize();
		dialogChanged();
		enabledPropertiesChanged();
		setControl(container);
	}
	
	/*
	 * This method is called in order to update the status message 
	 */
	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}
	
	/*
	 * This method is called in order to initialize the controls status
	 */
	private void initialize() {
		enabledPropertiesChanged();
	}
	
	/*
	 * This method is called when any 'enable' checkbox is checked
	 */
	private void enabledPropertiesChanged(){
		postToAllCombo.setEnabled(enablePostToAll.getSelection());
		/*randomSelCombo.setEnabled(enableRandomSel.getSelection());*/
		dialogChanged();
				
	}
	
	/*
	 * Ensures that all enabled text fields are set.
	 */
	private void dialogChanged() {
		messageEventTypeCombo.setEnabled(typeCombo.getText().equals("messageevent"));
		
		if(nameText.getText().length() == 0){
			updateStatus("A name must be specified for the new Event");
			return;
		}
			
		updateStatus(null);
	}

	/**
	 * 
	 * @return name for the new event
	 */
	public String getEventName() {
		return nameText.getText();
	}

	/**
	 * 
	 * @return type of event
	 */
	public String getEventType() {
		return typeCombo.getText();
	}

	/**
	 * 
	 * @return posttoall flag (if enabled)
	 */
	public String getPostToAllFlag() {
		if(postToAllCombo.isEnabled()){
			return postToAllCombo.getText();
		}
		return null;
	}

	
	/*
	 * 
	 * @return randomsel flag (if enabled)
	 */
	/*
	public String getRandomSelFlag() {
		if(randomSelCombo.isEnabled()){
			return randomSelCombo.getText();
		}
		return null;
	}
	*/
	
	public String getTypeOfMessageEvent(){
		if(typeCombo.getText().equals("messageevent")){
			return messageEventTypeCombo.getText();
		}
		return null;
	}
	
	
}